package com.manager.service.view.impl;


import com.manager.bean.PageInfo;
import com.manager.bean.view.DormitoryView;
import com.manager.bean.view.DormitoryViewExample;
import com.manager.mapper.view.DormitoryViewMapper;
import com.manager.service.view.DormitoryViewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service("dormitoryViewService")
public class DormitoryViewServiceImpl implements DormitoryViewService {
    @Resource
    private DormitoryViewMapper dormitoryViewDao;
    @Override
    public PageInfo<DormitoryView> findPageInfo(Integer Id, String Building, String Manager,Integer pageIndex, Integer pageSize) {
        PageInfo<DormitoryView> pi = new PageInfo<DormitoryView>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        DormitoryViewExample dormitoryViewExample=new DormitoryViewExample();
        DormitoryViewExample.Criteria criteria=dormitoryViewExample.createCriteria();
        if(Manager!=null&&Manager!=""){
            criteria.andManagerLike("%"+Manager+"%");
        }
        if(Id!=null){
            criteria.andIdEqualTo(Id);
        }
        if(Building!=null&&Building!=""){
            criteria.andBuildingLike(Building);
        }
        //获取总条数
        Integer totalCount = dormitoryViewDao.countByExample(dormitoryViewExample);
        if (totalCount>0){
            pi.setTotalCount(totalCount);
            //每一页显示学生信息数
            //currentPage = (pageIndex-1)*pageSize  当前页码数减1*最大条数=开始行数
            List<DormitoryView> classInfos =	dormitoryViewDao.selectByExample(dormitoryViewExample);
            int start=(pi.getPageIndex()-1)*pi.getPageSize();
            int end=start+pi.getPageSize();
            if(end>classInfos.size()-1){
                end=classInfos.size();
            }
            classInfos=classInfos.subList(start,end);
            pi.setList(classInfos);
        }
        return pi;
    }

    @Override
    public List<DormitoryView> getAll() {
        DormitoryViewExample dormitoryViewExample = new DormitoryViewExample();
        DormitoryViewExample.Criteria criteria = dormitoryViewExample.createCriteria();
        return dormitoryViewDao.selectByExample(dormitoryViewExample);
    }
}
